(function(w){
    function on_click(){
        var text = $(this).text();
        $('#input-item_type').val(text);

    }

    function on_tag_click(){
        $this = $(this);
        let id = $this.data('id');
        let container_id = $this.parent().attr('id');
        let new_container = container_id ==='tag_container';
        let item_id = $('input[name="id"]').val();
        $(!new_container?'#tag_container':'#tag_selected_container').append($this);
        $.post('/api/admin/tags/select',{id:id,add:new_container?1:0,item_id:item_id},function(data){

        });
    }

    function on_add_tag(){
        var type = $(this).data('type');
        $.get('/api/admin/tags',{'type':type},function(data){
            data = data.data;
            if(data){
                var html = '';
                data.forEach(function(item){
                    html += '<div class="btn btn-secondary tag-item" data-id="'+item.id+'">'+item.tag_name+':'+item.tag_value+' <i class="fa fa-close"></i></div>';
                });
                $('#tag_container').html(html);
                $('#tag_container .tag-item').on('click',on_tag_click);
            }
        });
        $('.add-tag').hide();
    }
    
    function on_attr_blur(){
        var $this = $(this);
        var item_id = $this.data('item_id');      
        var value_id = $this.data('id');
        var key = $this.attr('name');
        var value = $this.val();
        var old = $this.data('old');
        if(old===value){
            return;
        }
        $.post('/api/admin/attrs/valmap',{'item_id':item_id,'id':value_id,name:key,value:value},function(d){
            
        });
    }
    
    function on_select_item(){
        var html = '';
        function init_listener(){
            $('.js-digital-item').on('click',function(){
                var id = $(this).attr('id');
                var bid = $('input[name="id"]').val();
                $.post('/api/admin/store/set_digital',{'item_id':id,'booth_id':bid},function(){
                    reloadPage(window);
                });
                layer.closeAll();
            });
        }
        $.get('/api/admin/store/digital',function(d){
            for(var key in d.data){
                var item = d.data[key];
                html += '<a class="btn btn-primary js-digital-item" id="'+item['id']+'">'+item['item_name']+'</a>';
            }
            Wind.use('layer',function(){
                layer.open({
                    type: 1, // page 层类型
                    area: ['500px', '300px'],
                    title: '选择数字藏品',
                    shade: 0.6, // 遮罩透明度
                    shadeClose: true, // 点击遮罩区域，关闭弹层
                    maxmin: true, // 允许全屏最小化
                    anim: 0, // 0-6 的动画形式，-1 不开启
                    content: '<div style="padding: 32px;">'+html+'</div>',
                    success:init_listener
                });
            });
            
        });
        
        
    }
    
    function on_change(files){
        var imgs = $('#img_list').children('.img_item');
        var imglist = [];
        imgs.each(function(idx,img){
            var $img = $(img);
            var src = $img.data('src');
            $img.children('img').attr('src',src);
            imglist.push($img.data('path'));
        });
        var id = $('#item_id').val();
        var data = {'id':id,imgs:imglist};
        $.post('/api/admin/store/save_images',data,function(d){console.log(d);});
    }

    function init(){
        console.log('init');
        $('.add-tag').on('click',on_add_tag);
        $('.js-select-type').on('click',on_click);
        $('.tag-item').on('click',on_tag_click);
        $('.js-set_value').on('blur',on_attr_blur);
        $('.js-select_item').on('click',on_select_item);
        w.on_filechange = on_change;
    }
    init();
})(window);
